package com.xingdata.jjxc.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.ar_navi.ARHudComm;
import com.xingdata.jjxc.ar_navi.DeviceConnectionEvent;
import com.xingdata.jjxc.ar_navi.MotorControlParam;
import com.xingdata.jjxc.ar_navi.NaviOnBoardModeParam;
import com.xingdata.jjxc.ar_navi.ProtoMsg;
import com.xingdata.jjxc.ar_navi.Sysconfig;
import com.xingdata.jjxc.base.Base1Activity;
import com.xingdata.jjxc.utils.JUtils;
import com.xingdata.jjxc.utils.SharedPreTools;
import com.xingdata.jjxc.utils.SystemInfo;
import com.xingdata.jjxc.views.LongPressImageView;
import com.xingdata.jjxc.wifi.WifiAdapter;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;
import jar.MessageManager;
import jar.listener.ContentStateListener;
import jar.model.RequestEntity;
import jar.observer.CommunicationObserver;
import jar.tools.InstructionSet;
import jar.tools.UdpTools;

public class SettingsActivity extends Base1Activity implements OnClickListener, OnCheckedChangeListener {
	private String TAG = getClass().getSimpleName();
	/**连接hud*/
	private TextView hud_state;
	private Handler handler;
	private final int MESSAGECODE_CONTENT = 0;
	private final int MESSAGECODE_DISCONTENT = 1;

	private int[] groupIDs = { R.id.cof_navivoice,
			R.id.cof_obdmsg, R.id.cof_notifypush, R.id.cof_capture,
			R.id.cof_dislight, R.id.cof_notifyplay, R.id.cof_navipathtype};
	private Message message;
	private Intent intent;
	private Bundle bundle;
	private SeekBar seekbar_bright,seekbar_motor;
	private CheckBox cb_navi_instruction,cb_auto_brightness;

	private LinearLayout device_hotspot;
    private TextView     device_hotspot_state;
    private LinearLayout phone_hotspot;
    private TextView     phone_hotspot_state;

	// 导航模式控制参数
	private ImageView iv_open_navi_mode_setting;
	private View      navi_mode_setting_panel;
	private boolean show_navi_mode_setting_panel = false;
	private CheckBox cb_navi_simple_mode;
	private CheckBox cb_navi_notify_mode;
	private CheckBox cb_navi_route_info_mode;

    // 导航仿真调试模式  发送广播数据，并且不需要设备连接状态检查
    private CheckBox cb_navi_debug_data_mode;
    private CheckBox cb_navi_on_board_mode;// 车载自主导航模式

	// 电机1
	ImageView imageView_motor1_minus;
	ImageView imageView_motor1_plus;

	// 电机2
	ImageView imageView_motor2_minus;
	ImageView imageView_motor2_plus;

	TextView  tv_x3_demo_go_prev;
	TextView  tv_x3_demo_go_next;
	CheckBox  cb_open_x3_demo_mode;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        EventBus.getDefault().register(this);

		handler = new Handler() {
			public void handleMessage(android.os.Message msg) {

				switch (msg.what) {
				case MESSAGECODE_CONTENT:
                    setHudSpotConnState(true);
					break;

				case MESSAGECODE_DISCONTENT:
                    setHudSpotConnState(false);
					break;

				default:
					break;
				}
			};
		};
		initView();

        device_hotspot_state = (TextView)findViewById(R.id.hudspot_state) ;
		device_hotspot_state.setText("设备未连接");
		device_hotspot = (LinearLayout) findViewById(R.id.device_hotspot);
		device_hotspot.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//
//                refreshWifi();
				Intent intent = new Intent();
				bundle = new Bundle();
				bundle.putString("flag", "1");
				intent.setClass(SettingsActivity.this, WlanListView.class); //  打开wifi连接列表
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});



		TextView ContentWifi = (TextView)findViewById(R.id.ContentWifi) ;
        phone_hotspot_state = (TextView)findViewById(R.id.hint_connectwifi) ;
		phone_hotspot_state.setText(ARHudComm.getInstance().deviceIsConnected()?"设备已连接":"设备未连接");
        phone_hotspot = (LinearLayout) findViewById(R.id.phone_hotspot);
        phone_hotspot.setOnClickListener(phoneClickListener);
		ContentWifi.setOnClickListener(phoneClickListener);

		navi_mode_setting_panel = findViewById(R.id.navi_mode_setting_panel);

		iv_open_navi_mode_setting = (ImageView) findViewById(R.id.open_navi_mode_setting);
		iv_open_navi_mode_setting.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				show_navi_mode_setting_panel = !show_navi_mode_setting_panel;
				navi_mode_setting_panel.setVisibility(show_navi_mode_setting_panel?View.VISIBLE:View.GONE);
			}
		});

        cb_navi_simple_mode = (CheckBox) findViewById(R.id.cb_navi_simple_mode);
        cb_navi_notify_mode = (CheckBox) findViewById(R.id.cb_navi_notify_info_mode);
        cb_navi_route_info_mode = (CheckBox) findViewById(R.id.cb_navi_route_info_mode);

        cb_navi_simple_mode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.d(TAG, "简洁模式="+isChecked);
                if(isChecked){
                    cb_navi_notify_mode.setChecked(false);
                    cb_navi_route_info_mode.setChecked(false);
                }

				if(!cb_navi_simple_mode.isChecked()
						&&!cb_navi_notify_mode.isChecked()
						&&!cb_navi_route_info_mode.isChecked()){
					cb_navi_simple_mode.setChecked(true);
				}
			}
		});

        cb_navi_notify_mode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.d(TAG, "提示信息模式="+isChecked);
                if(isChecked){
                    cb_navi_simple_mode.setChecked(false);
                    cb_navi_route_info_mode.setChecked(false);
                }

				if(!cb_navi_simple_mode.isChecked()
						&&!cb_navi_notify_mode.isChecked()
						&&!cb_navi_route_info_mode.isChecked()){
					cb_navi_simple_mode.setChecked(true);
				}
            }
        });

        cb_navi_route_info_mode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.d(TAG, "路径信息模式="+isChecked);
                if(isChecked){
                    cb_navi_simple_mode.setChecked(false);
                    cb_navi_notify_mode.setChecked(false);
                }

				if(!cb_navi_simple_mode.isChecked()
						&&!cb_navi_notify_mode.isChecked()
						&&!cb_navi_route_info_mode.isChecked()){
					cb_navi_simple_mode.setChecked(true);
				}
            }
        });

		// 导航数据调试模式
		final Sysconfig naviConfig = new Sysconfig(this);
        cb_navi_debug_data_mode = (CheckBox)findViewById(R.id.cb_navi_data_debug_mode);
		cb_navi_debug_data_mode.setChecked(naviConfig.getNaviDataDebugMode());
		App.deviceSettings.onBoardNavi = naviConfig.getOnBoardNaviMode();

        cb_navi_debug_data_mode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
					ARHudComm.getInstance().enableNaviDebugMode();
                }else {
					ARHudComm.getInstance().disableNaviDebugMode();
				}
				naviConfig.setNaviDataDebugMode(isChecked);
            }
        });
		// 车载导航模式
        cb_navi_on_board_mode = (CheckBox)findViewById(R.id.cb_navi_on_board_mode);
		cb_navi_on_board_mode.setChecked(naviConfig.getOnBoardNaviMode());
        cb_navi_on_board_mode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				App.deviceSettings.onBoardNavi = isChecked;
				naviConfig.setOnBoardNaviMode(isChecked);
                ProtoMsg msg = new ProtoMsg("set_navi_on_board",
						JSON.toJSONString(new NaviOnBoardModeParam(isChecked)));
                ARHudComm.getInstance().sendToHud(msg.toBytes());
            }
        });

        // 电机1调节功能
        imageView_motor1_minus = (ImageView)findViewById(R.id.img_motor1_rotate_minus);
        imageView_motor1_minus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ProtoMsg msg = new ProtoMsg("set_motor1_rotate",
                        JSON.toJSONString(new MotorControlParam("0","1")));
                ARHudComm.getInstance().sendToHud(msg.toBytes());
            }
        });
        imageView_motor1_minus.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
				if(v instanceof LongPressImageView){
					int cnt = ((LongPressImageView) v).getLongClickCount()*5;
					while(cnt-- >0){
						ProtoMsg msg = new ProtoMsg("set_motor1_rotate",
								JSON.toJSONString(new MotorControlParam("0","1")));
						ARHudComm.getInstance().sendToHud(msg.toBytes());
					}
				}
                return true;
            }
        });

        imageView_motor1_plus = (ImageView)findViewById(R.id.img_motor1_rotate_plus);
        imageView_motor1_plus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ProtoMsg msg = new ProtoMsg("set_motor1_rotate",
                        JSON.toJSONString(new MotorControlParam("0","0")));
                ARHudComm.getInstance().sendToHud(msg.toBytes());
            }
        });
		// 长按事件
        imageView_motor1_plus.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
				if(v instanceof LongPressImageView){
					int cnt = ((LongPressImageView) v).getLongClickCount() *5;
					while(cnt-- >0){
						ProtoMsg msg = new ProtoMsg("set_motor1_rotate",
								JSON.toJSONString(new MotorControlParam("0","0")));
						ARHudComm.getInstance().sendToHud(msg.toBytes());
					}
				}
                return true;
            }
        });

        // 电机2调节功能
        imageView_motor2_minus = (ImageView)findViewById(R.id.img_motor2_rotate_minus);
        imageView_motor2_minus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ProtoMsg msg = new ProtoMsg("set_motor2_rotate",
                        JSON.toJSONString(new MotorControlParam("1","1")));
                ARHudComm.getInstance().sendToHud(msg.toBytes());
            }
        });
        imageView_motor2_minus.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                ProtoMsg msg = new ProtoMsg("set_motor2_rotate",
                        JSON.toJSONString(new MotorControlParam("1","1")));
				if(v instanceof LongPressImageView){
					int cnt = ((LongPressImageView) v).getLongClickCount();
					while(cnt-- >0){
						ARHudComm.getInstance().sendToHud(msg.toBytes());
					}
				}
                return false;
            }
        });

        imageView_motor2_plus = (ImageView)findViewById(R.id.img_motor2_rotate_plus);
        imageView_motor2_plus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ProtoMsg msg = new ProtoMsg("set_motor2_rotate",
                        JSON.toJSONString(new MotorControlParam("1","0")));
                ARHudComm.getInstance().sendToHud(msg.toBytes());
            }
        });
        imageView_motor2_plus.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                ProtoMsg msg = new ProtoMsg("set_motor2_rotate",
                        JSON.toJSONString(new MotorControlParam("1","0")));
				if(v instanceof LongPressImageView){
					int cnt = ((LongPressImageView) v).getLongClickCount();
					while(cnt-- >0){
						ARHudComm.getInstance().sendToHud(msg.toBytes());
					}
				}
                return true;
            }
        });

		tv_x3_demo_go_prev = (TextView)findViewById(R.id.tv_x3_demo_go_prev);
		tv_x3_demo_go_prev.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ProtoMsg msg = new ProtoMsg("x3_demo_go_prev","");
				ARHudComm.getInstance().sendToHud(msg.toBytes());
			}
		});
		tv_x3_demo_go_next = (TextView)findViewById(R.id.tv_x3_demo_go_next);
		tv_x3_demo_go_next.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ProtoMsg msg = new ProtoMsg("x3_demo_go_next","");
				ARHudComm.getInstance().sendToHud(msg.toBytes());
			}
		});
		cb_open_x3_demo_mode = (CheckBox) findViewById(R.id.cb_open_x3_obd_demo_mode);
		cb_open_x3_demo_mode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					ProtoMsg msg = new ProtoMsg("x3_open_demo_mode","");
					ARHudComm.getInstance().sendToHud(msg.toBytes());
				}else{
					ProtoMsg msg = new ProtoMsg("x3_close_demo_mode","");
					ARHudComm.getInstance().sendToHud(msg.toBytes());
				}
			}
		});

	}

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private OnClickListener phoneClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			bundle = new Bundle();
			bundle.putString("flag", "0");
			intent.setClass(SettingsActivity.this, HotspotSettingActivity.class);//lxs wifi ap setting
			intent.putExtras(bundle);
			startActivity(intent);
		}
	};

    void refreshWifi(){
        switch (WifiAdapter.InitInstance(SettingsActivity.this).checkWifi()){
            case WifiAdapter.Mode_AP:{
                setPhoneSpotConnState(true);
                setHudSpotConnState(false);
            }break;
            case WifiAdapter.Mode_Client:{
                setPhoneSpotConnState(false);
//                setHudSpotConnState(true);
            }break;
            case -1:{
                setPhoneSpotConnState(false);
                setHudSpotConnState(false);
            }break;
        }
    }

    void setPhoneSpotConnState(boolean isConnected){
        phone_hotspot_state.setText(isConnected?"已打开":"未打开");
    }
    void setHudSpotConnState(boolean isConnected){
        device_hotspot_state.setText(isConnected?"连接成功":"连接失败");
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void handleDeviceConnectionEvent(DeviceConnectionEvent event){
        if(event.id == DeviceConnectionEvent.Connected){
            phone_hotspot_state.setText("设备已连接");
            phone_hotspot_state.setTextColor(Color.BLACK);
        }
        if(event.id == DeviceConnectionEvent.Disconnected){
            phone_hotspot_state.setText("设备未连接");
            phone_hotspot_state.setTextColor(Color.RED);
        }
    }

	private void initView() {
		// 设备升级界面点击进入
		LinearLayout update_hud = (LinearLayout) findViewById(R.id.update_hud);
		update_hud.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(SettingsActivity.this, HudUpdateActivity.class);
//				Intent intent = new Intent(SettingsActivity.this, UpdateActivity.class);
				startActivity(intent);
			}
		});
		// 设置字体
		Typeface font = Typeface.createFromAsset(this.getAssets(), JUtils.TypefacePath);
		((TextView)findViewById(R.id.ContentWifi)).setTypeface(font);
		((TextView)findViewById(R.id.hint_connectwifi)).setTypeface(font);
//		((TextView)findViewById(R.id.ModifyHotspot)).setTypeface(font);
//		((TextView)findViewById(R.id.hint_passworld)).setTypeface(font);
		((TextView)findViewById(R.id.connect_HUD)).setTypeface(font);
		((TextView)findViewById(R.id.hudspot_state)).setTypeface(font);
		((TextView)findViewById(R.id.remote_control)).setTypeface(font);
		((TextView)findViewById(R.id.update_hud_text)).setTypeface(font);
		((TextView)findViewById(R.id.tv_01)).setTypeface(font);

		((Button)findViewById(R.id.ConnextKeyless)).setTypeface(font);
		((TextView)findViewById(R.id.tv_displaysetting)).setTypeface(font);

		((TextView)findViewById(R.id.birghtness_adjustment)).setTypeface(font);
		((RadioButton)findViewById(R.id.brightness_low_rb)).setTypeface(font);
		((RadioButton)findViewById(R.id.brightness_middle_rb)).setTypeface(font);
		((RadioButton)findViewById(R.id.brightness_high_rb)).setTypeface(font);
		
		((TextView)findViewById(R.id.SMS_push)).setTypeface(font);
		((RadioButton)findViewById(R.id.SMSpush_close_rb)).setTypeface(font);
		((RadioButton)findViewById(R.id.SMSpush_open_rb)).setTypeface(font);
		
		((TextView)findViewById(R.id.OBD_display)).setTypeface(font);
		((RadioButton)findViewById(R.id.OBD_close_rb)).setTypeface(font);
		((RadioButton)findViewById(R.id.OBD_open_rb)).setTypeface(font);
		
		((TextView)findViewById(R.id.navi_setting)).setTypeface(font);
		((RadioButton)findViewById(R.id.route_planning_shortest_time_rb)).setTypeface(font);
		((RadioButton)findViewById(R.id.route_planning_avoid_gridlock_rb)).setTypeface(font);
		((RadioButton)findViewById(R.id.route_planning_avoid_charge__rb)).setTypeface(font);
		
		((TextView)findViewById(R.id.voice_broadcast_setting)).setTypeface(font);
		((TextView)findViewById(R.id.navi_broadcast)).setTypeface(font);
		((RadioButton)findViewById(R.id.navi_broadcast_close_rb)).setTypeface(font);
		((RadioButton)findViewById(R.id.navi_broadcast_open_rb)).setTypeface(font);
		((TextView)findViewById(R.id.broadcast_message)).setTypeface(font);
		((RadioButton)findViewById(R.id.broadcast_message_close_rb)).setTypeface(font);
		((RadioButton)findViewById(R.id.broadcast_message_open_rb)).setTypeface(font);
		((TextView)findViewById(R.id.drive_recorder_setting)).setTypeface(font);
		
		((TextView)findViewById(R.id.video_length)).setTypeface(font);
		((RadioButton)findViewById(R.id.video_length_6s_rb)).setTypeface(font);
		((RadioButton)findViewById(R.id.video_length_9s_rb)).setTypeface(font);
		((RadioButton)findViewById(R.id.WangXin_SWITCH)).setTypeface(font);
		((TextView)findViewById(R.id.adjust_motor_text)).setTypeface(font);
		((TextView)findViewById(R.id.motor1_setting)).setTypeface(font);
		((TextView)findViewById(R.id.motor2_setting)).setTypeface(font);

		((TextView)findViewById(R.id.tv_displaysetting_2)).setTypeface(font);

		((TextView)findViewById(R.id.text_navi_mode)).setTypeface(font);
		((TextView)findViewById(R.id.text_simple_mode)).setTypeface(font);
		((TextView)findViewById(R.id.text_notify_info)).setTypeface(font);
		((TextView)findViewById(R.id.text_route_mode)).setTypeface(font);
		((TextView)findViewById(R.id.text_navi_debug_data_mode)).setTypeface(font);
		((TextView)findViewById(R.id.text_navi_on_board)).setTypeface(font); // 车载自主导航模式

		findViewById(R.id.ConnextKeyless).setOnClickListener(this);
		//findViewById(R.id.ModifyHotspot).setOnClickListener(this);
		findViewById(R.id.ContentWifi).setOnClickListener(this);

		if (UdpTools.isContent) {
			message = new Message();
			message.what = MESSAGECODE_CONTENT;
			handler.sendMessage(message);
		} else {
			message = new Message();
			message.what = MESSAGECODE_DISCONTENT;
			handler.sendMessage(message);
		}

		CommunicationObserver.addContentStateListener(new ContentStateListener() {

			@Override
			public void onContentChange(int arg0) {
				switch (arg0) {
				case 1:
					message = new Message();
					message.what = MESSAGECODE_CONTENT;
					handler.sendMessage(message);
					break;
				case 2:
					message = new Message();
					message.what = MESSAGECODE_DISCONTENT;
					handler.sendMessage(message);
					break;

				default:
					break;
				}

			}
		});
		initSharedValue(groupIDs);
		initMotorAdjustRadio();
	}
	
	private void initMotorAdjustRadio() {
		RadioGroup group = (RadioGroup) findViewById(R.id.motor_adjust);
		String groupTag = group.getTag().toString();
		String sharedValue = SharedPreTools.getString(this, groupTag, "");
		RadioButton radioButton;
		if (sharedValue != null && sharedValue.length() != 0) {
			radioButton = (RadioButton) group.findViewWithTag(sharedValue);
		} else {
			radioButton = ((RadioButton) group.getChildAt(0));
		}
		radioButton.setChecked(true);
		group.setOnCheckedChangeListener(this);
	}
	
	@Override
	protected int getContentViewId() {
		return R.layout.activity_setting_params;
	}

	@Override
	protected String getTitleText() {
		return "设置";
	}

	@SuppressLint("NewApi")
	@Override
	protected void initViews() {
		initRadioGroup(groupIDs);
		initSeekBarAndCheckBox();
	}

    private static class LightSettings{
        public int min = 0;
        public int max = 255;
        public int value;
        LightSettings(){

        }
        LightSettings(int value){
            this.value = value;
        }
    }


	public class NetworkChangeReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(final Context context, final Intent intent) {
			final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			final android.net.NetworkInfo activeNetwork = connMgr.getActiveNetworkInfo();
			if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI){

			}


		}
	}


	private void initSeekBarAndCheckBox() {
//		cb_navi_instruction = (CheckBox) findViewById(R.id.cb_navi_instruction);
		cb_auto_brightness = (CheckBox) findViewById(R.id.cb_auto_brightness);

		seekbar_bright = (SeekBar) findViewById(R.id.seekbar_birght);
		seekbar_motor = (SeekBar) findViewById(R.id.seekbar_motor);

		seekbar_bright.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {


			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {

			}



			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
                Log.d(TAG, "亮度调节发送数据="+seekBar.getProgress());
                ARHudComm.getInstance().send2Hud(ARHudComm.HudAction.ADJUST_LIGHT,new LightSettings(seekBar.getProgress()));
			}
		});

		seekbar_motor.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {


			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {

			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
                Log.d(TAG, "马达调节发送数据="+seekBar.getProgress());

			}
		});

//		cb_navi_instruction.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//			@Override
//			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                Log.d(TAG, "导航指示信息="+isChecked);
//                ARHudComm.getInstance().send2Hud(ARHudComm.HudAction.NAV_INDICATOR,new Boolean(isChecked));
//			}
//		});

		cb_auto_brightness.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.d(TAG, "自动亮度设置="+isChecked);
                ARHudComm.getInstance().send2Hud(ARHudComm.HudAction.AUTO_ADJUST_LIGHT,new Boolean(isChecked));

			}
		});
	}

	private void initSharedValue(int[] ids) {
		for (int id : ids) {
			View view = findViewById(id);
			if (view instanceof RadioGroup) {
				RadioGroup group = (RadioGroup) view;
				String groupTag = group.getTag().toString();
				String sharedValue = SharedPreTools.getString(this, groupTag, "");
				RadioButton radioButton;
				if (sharedValue != null && sharedValue.length() != 0) {
					radioButton = (RadioButton) group.findViewWithTag(sharedValue);
				} else {
					if (id == R.id.cof_capture || id == R.id.cof_dislight
							|| id == R.id.cof_navipathtype) {
						radioButton = ((RadioButton) group.getChildAt(0));
					} else {
						radioButton = ((RadioButton) group.getChildAt(1));
					}

				}
				if (radioButton != null) {
					radioButton.setChecked(true);
				}

			}

		}

	}

	private void initRadioGroup(int[] ids) {
		for (int i = 0; i < ids.length; i++) {
			RadioGroup radioGroup = (RadioGroup) findViewById(ids[i]);
			radioGroup.setOnCheckedChangeListener(this);
			// ((RadioButton) radioGroup.getChildAt(0)).setChecked(true);
		}

	}

	@SuppressLint("NewApi")
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		// 连接遥控器
		case R.id.ConnextKeyless:
			// JSONObject jsonObject = new JSONObject();
			// jsonObject.put(key, value)

			MessageManager.send(Integer.parseInt(SystemInfo.getSendPORT()), new RequestEntity("",
					InstructionSet.INSTRUCTION_AUTH_KEYLESS_TO_HUD + ""));
			showKeylessDialog();

			break;


		case R.id.ContentWifi:


			break;
		default:
			break;
		}
	}

//	boolean isFirst = true;
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// //如果不是点击事件跳出
		// if (!group.isPressed()) {
		// return;
		// }
//		if (isFirst) {
//			isFirst = false;
//		} else {
//			RadioButton radioButton = (RadioButton) group
//					.findViewById(checkedId);
//			for (int i = 0; i < group.getChildCount(); i++) {
//				RadioButton mButton = (RadioButton) group.getChildAt(i);
//				if (!mButton.isChecked()) {
//					// mButton.setTextColor(getResources().getColor(R.color.txt_black1));
//				} else {
//					// mButton.setTextColor(getResources().getColor(R.color.white));
//				}
//
//			}
//			if (group.getTag() != null && radioButton.getTag() != null) {
//				setSharedValue(group.getTag().toString(), radioButton.getTag()
//						.toString());
//				if (group.getTag().equals("motor_adjust")) {
//					MessageManager.send(Integer.parseInt(SystemInfo
//							.getSendPORT()), new RequestEntity(radioButton
//							.getTag().toString(), 23032 + ""));
//					Log.d(TAG, "亮度TAG:" + radioButton.getTag().toString());
//				} else {
//					sendToHUD();
//				}
//			}
//
//		}
	}
//
//	/**
//	 * 获取对应的Shared Key值
//	 *
//	 */
//	private void sendToHUD() {
//		JSONObject jsonObject = new JSONObject();
//		String key = null;
//		for (int id : groupIDs) {
//			RadioGroup group = (RadioGroup) findViewById(id);
//			int checkID = group.getCheckedRadioButtonId();
//			if (checkID == -1) {
//				return;
//			}
//			String strTag = group.findViewById(checkID).getTag().toString();
//			switch (id) {
//			// 亮度显示
//			case R.id.cof_dislight:
//				key = Common.COF_DISLIGHT;
//				break;
//			// 是否推送消息
//			case R.id.cof_notifypush:
//				key = Common.COF_NOTIFYPUSH;
//				break;
//			// 导航路线规划：时间最短-0、躲避拥堵-4、避开收费-1
//			case R.id.cof_navipathtype:
//				key = Common.COF_NAVIPATHTYPE;
//				break;
//			// 导航语音播报
//			case R.id.cof_navivoice:
//				key = Common.COF_NAVIVOICE;
//				break;
//			// 播报消息开关
//			case R.id.cof_notifyplay:
//				key = Common.COF_NOTIFYPLAY;
//				break;
//			// 是否显示OBD
//			case R.id.cof_obdmsg:
//				key = Common.COF_OBDMSG;
//				break;
//			// 行车记录仪显示
//			case R.id.cof_capture:
//				key = Common.COF_CAPTURE;
//				break;
//			default:
//				break;
//			}
//			jsonObject.put(key, strTag);
//
//		}
//		Log.i(TAG,"send setting data:"+jsonObject.toJSONString());
//		MessageManager.send(Integer.parseInt(SystemInfo.getSendPORT()), new RequestEntity(jsonObject.toJSONString(),
//				InstructionSet.INSTRUCTION_AUTH_OPTIONS_TO_HUD + ""));
//	}

	/**
	 * 根据 id 设置Shared值
	 * 
	 * @param key
	 * @param value
	 */
	private void setSharedValue(String key, String value) {
		Log.i(TAG, "SharedPreferences >" + "value: " + key + "value: " + value);
		SharedPreTools.putString(this, key, value);
	}

	//
	// @Override
	// public void onCheckedChanged(CompoundButton buttonView, boolean
	// isChecked) {
	// String strKey = buttonView.getTag().toString();
	// String strValue = "";
	// if (isChecked) {
	// strValue = "0";
	// buttonView.setTextColor(getResources().getColor(R.color.white));
	// buttonView.setText("开");
	//
	// } else {
	// strValue = "9";
	// buttonView.setTextColor(getResources().getColor(R.color.green));
	// buttonView.setText("");
	// }
	// setSharedValue(strKey, strValue);
	//
	// }

	// private String getJJsetting() {
	// StringBuffer strSetting = new StringBuffer();
	// for (int i = 0; i < SystemInfo.Settings.length; i++) {
	// if (strSetting.length() != 0) {
	// strSetting.append("&");
	// }
	// strSetting.append(SharedPreTools.getString(this,
	// SystemInfo.Settings[i], "0"));
	// }
	// return strSetting.toString();
	// };

	public void onBack(View view) {
		// JSONObject jsonObject = new JSONObject();
		// // 导航语音播报
		// jsonObject.put(Common.COF_NAVIVOICE, "9");
		// // 是否显示OBD
		// jsonObject.put(Common.HUDSHOWOBDDATAFLAG, "9");
		// // 是否推送消息
		// jsonObject.put(Common.PUSHNOTIFYAUDIOPLAYFLAG, "9");
		// // 行车记录仪 抓拍到计时0 3 6 9
		// jsonObject.put(Common.CAPTURECOUNTDOWNFLAG, "9");
		// // 亮度显示 3 5 8
		// jsonObject.put(Common.HUDDISPLAYLIGHTFLAG, "3");
		// // 消息推送开关
		// jsonObject.put(Common.HUDPUSHNOTIFYFLAG, "9");
		// // 导航路线规划：时间最短-0、躲避拥堵-4、避开收费-1
		// jsonObject.put(Common.NAVIPLANROUTERTYPEFLAG, "0");
		// MessageManager.send(Integer.parseInt(SystemInfo.getSendPORT()),
		// new RequestEntity(jsonObject.toJSONString(),
		// InstructionSet.INSTRUCTION_AUTH_OPTIONS_TO_HUD + ""));

	}

	private void showKeylessDialog() {

		final Dialog exitDialog = new Dialog(this, R.style.my_dialog);
		View v = LayoutInflater.from(this).inflate(R.layout.dialog_connecthud, null);
//		((TextView) v.findViewById(R.id.dialog_text)).setText("按下遥控任意键...");
		v.findViewById(R.id.close).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				exitDialog.dismiss();
			}
		});
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		exitDialog.addContentView(v, params);
		exitDialog.show();
	}

	private void showWifiDialog() {

		final Dialog exitDialog = new Dialog(this, R.style.my_dialog);
		View v = LayoutInflater.from(this).inflate(R.layout.dialog_wifi_hotspot, null);

		v.findViewById(R.id.dialog_cancel).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				exitDialog.dismiss();
			}
		});

		v.findViewById(R.id.dialog_continue).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

			}
		});

		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		exitDialog.addContentView(v, params);
		exitDialog.show();
	}

}
